package com.wawei.seeds.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.me.AppointmentMeFragment;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.WalnutMyServiceServiceHandler;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 * Created by t27891 on 15/7/24.
 */
public class WalnutAppointmentMeFragmentAdapter extends SeedsBaseAdapter<WalnutMainEvent> {
    public WalnutAppointmentMeFragmentAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder viewHolder;
        if(null == view){
            view = LayoutInflater.from(context).inflate(R.layout.fragment_walnut_me_appointment_me_item, parent, false);
            viewHolder = new ViewHolder(view);
            viewHolder.agreeAction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    agreeTheAppointment(viewHolder, (WalnutMainEvent)v.getTag());
                }
            });

            viewHolder.denyAction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    denyTheAgreement(viewHolder, (WalnutMainEvent) v.getTag());
                }
            });

            viewHolder.mansPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phoneNumber = ((TextView)v).getText().toString();
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });

            view.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder)view.getTag();
        }

        WalnutMainEvent item = (WalnutMainEvent)getItem(position);
        viewHolder.eventTitle.setText(item.title);
        viewHolder.mansPhone.setText(item.phone);
        viewHolder.appointmentMan.setText(item.booked);

        viewHolder.agreeAction.setTag(item);
        viewHolder.denyAction.setTag(item);

        if(null == item.publisherHeadPortrait || item.publisherHeadPortrait.length() < 1){
            imageLoader.loadImageByResources(R.drawable.walnut_logo, viewHolder.heroLogo, true, false);
        }
        else {
            imageLoader.loadImage(item.publisherHeadPortrait, viewHolder.heroLogo, true, false);
        }

        return view;
    }

    private void agreeTheAppointment(final ViewHolder viewHolder, WalnutMainEvent item){
        AppointmentDealNotifyHandler notifyHandler = new AppointmentDealNotifyHandler(viewHolder);
        WalnutMyServiceServiceHandler serviceServiceHandler = new WalnutMyServiceServiceHandler(context);
        serviceServiceHandler.acceptBooking(RetrofitNetworkService.makeAsyncRestAdapter(), item.bookingId, notifyHandler, AppointmentMeFragment.APPOINTMENT_AGREE);
    }

    private void denyTheAgreement(final ViewHolder viewHolder, WalnutMainEvent item){
        AppointmentDealNotifyHandler notifyHandler = new AppointmentDealNotifyHandler(viewHolder);
        WalnutMyServiceServiceHandler serviceServiceHandler = new WalnutMyServiceServiceHandler(context);
        serviceServiceHandler.cancelBooking(RetrofitNetworkService.makeAsyncRestAdapter(), item.bookingId, notifyHandler, AppointmentMeFragment.APPOINTMENT_DENY);
    }

    public static class ViewHolder{
        @InjectView(R.id.item_appointment_hero_logo)ImageView heroLogo;
        @InjectView(R.id.item_appointment_event_title)TextView eventTitle;
        @InjectView(R.id.item_appointment_man)TextView appointmentMan;
        @InjectView(R.id.item_appointment_agree)TextView agreeAction;
        @InjectView(R.id.item_appointment_deny)TextView denyAction;
        @InjectView(R.id.item_appointment_phone)TextView mansPhone;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    public static class AppointmentDealNotifyHandler extends WalnutBaseNofityHandler<ViewHolder, NetBase>{
        public AppointmentDealNotifyHandler(ViewHolder viewHolder) {
            super(viewHolder);
        }

        @Override
        public void handleOtherMessage(Message msg) {
            Bundle data = msg.getData();
            if(msg.what == AppointmentMeFragment.APPOINTMENT_AGREE){
                if(null != data.getString(RetrofitNetworkService.RESULT_ERROR) &&
                       data.getString(RetrofitNetworkService.RESULT_ERROR).equals(RetrofitNetworkService.RESULT_ERROR_NONE) ) {
                    get().agreeAction.setText("已约");
                    get().agreeAction.setEnabled(false);
                    get().denyAction.setVisibility(View.INVISIBLE);
                }
            }
            else if(msg.what == AppointmentMeFragment.APPOINTMENT_DENY){
                if(null != data.getString(RetrofitNetworkService.RESULT_ERROR) &&
                        data.getString(RetrofitNetworkService.RESULT_ERROR).equals(RetrofitNetworkService.RESULT_ERROR_NONE) ) {
                    get().agreeAction.setText("已取消");
                    get().agreeAction.setEnabled(false);
                    get().denyAction.setVisibility(View.INVISIBLE);
                }
            }
        }
    }
}
